package com.jyu.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jyu.reggie.common.CustomException;
import com.jyu.reggie.entity.Category;
import com.jyu.reggie.entity.Dish;
import com.jyu.reggie.entity.Employee;
import com.jyu.reggie.entity.Setmeal;
import com.jyu.reggie.mapper.CategoryMapper;
import com.jyu.reggie.mapper.EmployeeMapper;
import com.jyu.reggie.service.CategoryService;
import com.jyu.reggie.service.DishService;
import com.jyu.reggie.service.EmployeeService;
import com.jyu.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private SetmealService setmealService;

    @Autowired
    private DishService dishService ;

    @Override
    public void remove(Long id) {
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();

        queryWrapper.eq(Dish::getCategoryId, id);

        int count = dishService.count(queryWrapper);

        if( count > 0) {
            throw new CustomException("当下分类关联了菜品，不能删除");

        }

        LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<>();

        queryWrapper2.eq(Setmeal::getCategoryId, id);

        int count2 = setmealService.count(queryWrapper2);

        if( count2 > 0) {
            throw new CustomException("当下分类关联了套餐，不能删除");
        }

        super.removeById(id);

    }

}
